package com.tueye.web.controller.publishtopic;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tueye.base.util.IBaseHbnDao;
import com.tueye.cache.TuEyeCache;
import com.tueye.entity.Adinfo;
import com.tueye.service.IAdinfoService;
import com.tueye.service.IAdsiteService;
import com.tueye.service.ITopicService;
import com.tueye.utils.TuEyeConstant;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;


/**
 *  Title: UploadFileController.java
 *  Description: upload the imagess.
 * @author yuyue
 * @version 0.5 (for test)
 * 
 */
@SuppressWarnings("unchecked")
@Controller
public class indexController {

	Log log = LogFactory.getLog(indexController.class);
	String file_spe = File.separator;
	@Autowired
	@Qualifier("topicService")
	ITopicService topicService;

	@Autowired
	@Qualifier("tueyecache")
	TuEyeCache tueyecache;
	@Autowired
	@Qualifier("adinfoService")
	private IAdinfoService adinfoService;
	@Autowired
	@Qualifier("adsiteService")
	private IAdsiteService adsiteService;
	@Autowired
	@Qualifier("baseHbnDao")
	private IBaseHbnDao  baseHbnDao;
	@RequestMapping("/indexcreate.do") 
	public String createTypeList(HttpServletRequest request, HttpServletResponse response){ 
		//拿到tueye cache 里面所有的type
		boolean  retult = false;
		response.setContentType("html/txt");
		response.setCharacterEncoding("utf-8");
		try{ 
		 
			//查一次，知道有多少页，生成index.html
			String htmlFileName = TuEyeConstant.PROJECTPATH+file_spe +  "index.html"; 
			String templatePath = TuEyeConstant.PROJECTPATH +file_spe+"WEB-INF"+ file_spe + "template";
			Configuration cfg = null;
			cfg = new Configuration();
			cfg.setDefaultEncoding("utf-8"); 
			cfg.setDirectoryForTemplateLoading(new File(templatePath));
			cfg.setObjectWrapper(new DefaultObjectWrapper()); 
			Template temp = cfg.getTemplate("index.ftl");
			
			 
			HashMap contents = new HashMap();
			contents.put("liandong",getLiandong());
			contents.put("hd_title",gethd_title("top2",4));
			contents.put("huandeng",gethuandeng("top2",4));
			contents.put("body1",getbody1("图片1",4));
			contents.put("body2",getbody1("图片2",2));
			contents.put("body3",getbody1("图片3",2));
			contents.put("gundong1",getbody1("滚动1",8));
			contents.put("gundong2",getbody1("滚动2",8));
			contents.put("body4",getbody1("图片4",4));
			contents.put("body5",getbody1("图片5",2));
			contents.put("body6",getbody1("图片6",2));
			contents.put("left1",getbianlan("左边栏1",5));
			contents.put("left2",getbianlan("左边栏2",12));
			contents.put("left3",getbianlan("左边栏3",12));
			contents.put("left4",getbianlan("左边栏4",12));
			Writer out = new OutputStreamWriter(new FileOutputStream(htmlFileName),"utf-8");
			 temp.process(contents, out);
			 out.flush();
			 out.close();
			 retult = true;
			 com.tueye.ajax.json.JSONObject object = new com.tueye.ajax.json.JSONObject();
			 object.put("success", retult);
			response.getWriter().print(object.toString());
		}catch (Exception e) {
			e.printStackTrace(); 
		}
		
		return null;
		    
	}
	
	
	
 


//取图片

	private String getbody1(String site, int count) {
		// TODO Auto-generated method stub
		String hql=this.getsql(site);
		List body = baseHbnDao.queryList(hql);
		StringBuffer sb = new StringBuffer();
		if(body.size()>count){
			for(int i=0; i<count; i++) {
				if(i==0){
				Adinfo adinfo = (Adinfo) body.get(i);
				if("2".equals(adinfo.getPicture_site_type())){
					sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\" title='"+adinfo.getAdinfo_title()+"'><img src=\""+adinfo.getAdpicture_site()+"\" title='"+adinfo.getAdinfo_title()+"' alt='"+adinfo.getAdinfo_title()+"' width=\"148\" height=\"146\" /></a><div id=\"picDesc\"> ")
					.append("<a href=\""+adinfo.getAdinfo_address()+"\" title='"+adinfo.getAdinfo_title()+"'>"+adinfo.getAdinfo_title()+"</a></div></li>");
					
				}
				if("1".equals(adinfo.getPicture_site_type())){	
					
					sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\" title='"+adinfo.getAdinfo_title()+"'><img src=\"./pages/topics/aduploadimg/"+adinfo.getAdpicture_site()+"\" title='"+adinfo.getAdinfo_title()+"' alt='"+adinfo.getAdinfo_title()+"' width=\"146\" height=\"148\"/></a><div id=\"picDesc\"> ")
					.append("<a href=\""+adinfo.getAdinfo_address()+"\" title='"+adinfo.getAdinfo_title()+"'>"+adinfo.getAdinfo_title()+"</a></div></li>");
				}
				}else{
					Adinfo adinfo = (Adinfo) body.get(i);
					if("2".equals(adinfo.getPicture_site_type())){

						sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\" title='"+adinfo.getAdinfo_title()+"'><img src=\""+adinfo.getAdpicture_site()+"\" title='"+adinfo.getAdinfo_title()+"' alt='"+adinfo.getAdinfo_title()+"' width=\"148\" height=\"146\"/></a><div id=\"picDesc\"> ")
						.append("<a href=\""+adinfo.getAdinfo_address()+"\">"+adinfo.getAdinfo_title()+"</a></div></li>");
						
					}
					if("1".equals(adinfo.getPicture_site_type())){	
						
						sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\"><img src=\"./pages/topics/aduploadimg/"+adinfo.getAdpicture_site()+"\" title='"+adinfo.getAdinfo_title()+"' alt='"+adinfo.getAdinfo_title()+"' width=\"148\" height=\"146\"/></a><div id=\"picDesc\"> ")
						.append("<a href=\""+adinfo.getAdinfo_address()+"\">"+adinfo.getAdinfo_title()+"</a></div></li>");
					}
				}
			}
		}else{
			for(int i=0; i<body.size(); i++) {
				System.out.println(i);
				if(i==0){
					Adinfo adinfo = (Adinfo) body.get(i);
					if("2".equals(adinfo.getPicture_site_type())){

						sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\"><img src=\""+adinfo.getAdpicture_site()+"\" title='"+adinfo.getAdinfo_title()+"' alt='"+adinfo.getAdinfo_title()+"' width=\"148\" height=\"146\"/></a><div id=\"picDesc\"> ")
						.append("<a href=\""+adinfo.getAdinfo_address()+"\" title='"+adinfo.getAdinfo_title()+"'>"+adinfo.getAdinfo_title()+"</a></div></li>");
									
					}
					if("1".equals(adinfo.getPicture_site_type())){	
						
						sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\"><img src=\"./pages/topics/aduploadimg/"+adinfo.getAdpicture_site()+"\" title='"+adinfo.getAdinfo_title()+"' alt='"+adinfo.getAdinfo_title()+"' width=\"148\" height=\"146\"/></a><div id=\"picDesc\"> ")
						.append("<a href=\""+adinfo.getAdinfo_address()+"\" title='"+adinfo.getAdinfo_title()+"'>"+adinfo.getAdinfo_title()+"</a></div></li>");
				
					}
					}else{
						Adinfo adinfo = (Adinfo) body.get(i);
						if("2".equals(adinfo.getPicture_site_type())){

							sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\"><img src=\""+adinfo.getAdpicture_site()+"\" title='"+adinfo.getAdinfo_title()+"' alt='"+adinfo.getAdinfo_title()+"' width=\"148\" height=\"146\"/></a><div id=\"picDesc\"> ")
							.append("<a href=\""+adinfo.getAdinfo_address()+"\" title='"+adinfo.getAdinfo_title()+"'>"+adinfo.getAdinfo_title()+"</a></div></li>");
							
						}
						if("1".equals(adinfo.getPicture_site_type())){	
						
							sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\"><img src=\"./pages/topics/aduploadimg/"+adinfo.getAdpicture_site()+"\" title='"+adinfo.getAdinfo_title()+"' alt='"+adinfo.getAdinfo_title()+"' width=\"148\" height=\"146\"/></a><div id=\"picDesc\"> ")
							.append("<a href=\""+adinfo.getAdinfo_address()+"\" title='"+adinfo.getAdinfo_title()+"'>"+adinfo.getAdinfo_title()+"</a></div></li>");
						}
					}
			}
		}
	//	System.out.println("@@@@@@"+sb.toString());
		return sb.toString();
	}

//取边栏链接
	private String getbianlan(String site, int count) {
		// TODO Auto-generated method stub
		String hql=this.getsql(site);
		List bianlan = baseHbnDao.queryList(hql);
		StringBuffer sb = new StringBuffer();
		if(bianlan.size()>count){
			for(int i=0; i<count; i++) {
				Adinfo adinfo = (Adinfo) bianlan.get(i);
				sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\" title='"+adinfo.getAdinfo_title()+"'>"+adinfo.getAdinfo_title()+"</a></li>");
			}
		}else{
			for(int i=0; i<bianlan.size(); i++) {
				Adinfo adinfo = (Adinfo) bianlan.get(i);
				sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\" title='"+adinfo.getAdinfo_title()+"'>"+adinfo.getAdinfo_title()+"</a></li>");
			}
		}
		return sb.toString();
	}

//取幻灯片标题
	private String gethd_title(String site, int count) {
		// TODO Auto-generated method stub
		String hql=this.getsql(site);
		List huandeng = baseHbnDao.queryList(hql);
		StringBuffer sb = new StringBuffer();
		if(huandeng.size()>count){
			for(int i=0; i<count; i++) {
				if(i==0){
				Adinfo adinfo = (Adinfo) huandeng.get(i);
				sb.append("<li style=\"display:block;\">"+adinfo.getAdinfo_title()+"</li>");
				}else{
					Adinfo adinfo = (Adinfo) huandeng.get(i);
					sb.append("<li>"+adinfo.getAdinfo_title()+"</li>");
				}
			}
		}else{
			for(int i=0; i<huandeng.size(); i++) {
				if(i==0){
				Adinfo adinfo = (Adinfo) huandeng.get(i);
				sb.append("<li style=\"display:block;\">"+adinfo.getAdinfo_title()+"</li>");
				}else{
					Adinfo adinfo = (Adinfo) huandeng.get(i);
					sb.append("<li>"+adinfo.getAdinfo_title()+"</li>");
				}
			}
		}
		return sb.toString();
	}


//取幻灯片图片

	private String gethuandeng(String site,int count) {
		// TODO Auto-generated method stub
		String hql=this.getsql(site);
		List huandeng = baseHbnDao.queryList(hql);
		StringBuffer sb = new StringBuffer();
		if(huandeng.size()>count){
			for(int i=0; i<count; i++) {
				if(i==0){
				Adinfo adinfo = (Adinfo) huandeng.get(i);
				if("2".equals(adinfo.getPicture_site_type())){
					sb.append("<a href=\""+adinfo.getAdinfo_address()+"\"> <img style=\"display:block;\" ")
					.append("src=\""+adinfo.getAdpicture_site()+"\" alt=\""+adinfo.getAdinfo_title()+"\" title=\""+adinfo.getAdinfo_title()+"\"")
					.append("alt=\""+adinfo.getAdinfo_title()+"\" width='312px' height='178px' /></a> ");
					
				}
				if("1".equals(adinfo.getPicture_site_type())){	
					
					sb.append("<a href=\""+adinfo.getAdinfo_address()+"\"> <img style=\"display:block;\" ")
					.append("src=\"./pages/topics/aduploadimg/"+adinfo.getAdpicture_site()+"\" alt=\""+adinfo.getAdinfo_title()+"\" title=\""+adinfo.getAdinfo_title()+"\"")
					.append("alt=\""+adinfo.getAdinfo_title()+"\" width='312px' height='178px' /></a> ");
					
				}
				}else{
					Adinfo adinfo = (Adinfo) huandeng.get(i);
					if("2".equals(adinfo.getPicture_site_type())){
					
						
						sb.append("<a href=\""+adinfo.getAdinfo_address()+"\"> <img ")
						.append("src=\""+adinfo.getAdpicture_site()+"\" alt=\""+adinfo.getAdinfo_title()+"\" title=\""+adinfo.getAdinfo_title()+"\"")
						.append("alt=\""+adinfo.getAdinfo_title()+"\" width='312px' height='178px' /></a> ");
						
					}
					if("1".equals(adinfo.getPicture_site_type())){	
						
						sb.append("<a href=\""+adinfo.getAdinfo_address()+"\"> <img ")
						.append("src=\"./pages/topics/aduploadimg/"+adinfo.getAdpicture_site()+"\" alt=\""+adinfo.getAdinfo_title()+"\" title=\""+adinfo.getAdinfo_title()+"\"")
						.append("alt=\""+adinfo.getAdinfo_title()+"\" width='312px' height='178px' /></a> ");
					}
				}
			}
		}else{
			for(int i=0; i<huandeng.size(); i++) {
				System.out.println(i);
				if(i==0){
					Adinfo adinfo = (Adinfo) huandeng.get(i);
					if("2".equals(adinfo.getPicture_site_type())){
					
						
						sb.append("<a href=\""+adinfo.getAdinfo_address()+"\"> <img style=\"display:block;\" ")
						.append("src=\""+adinfo.getAdpicture_site()+"\" alt=\""+adinfo.getAdinfo_title()+"\" title=\""+adinfo.getAdinfo_title()+"\"")
						.append("alt=\""+adinfo.getAdinfo_title()+"\" width='312px' height='178px'/></a> ");
						
					}
					if("1".equals(adinfo.getPicture_site_type())){	
						
						sb.append("<a href=\""+adinfo.getAdinfo_address()+"\"> <img style=\"display:block;\" ")
						.append("src=\"./pages/topics/aduploadimg/"+adinfo.getAdpicture_site()+"\" alt=\""+adinfo.getAdinfo_title()+"\" title=\""+adinfo.getAdinfo_title()+"\"")
						.append("alt=\""+adinfo.getAdinfo_title()+"\" width='312px' height='178px'/></a> ");
				
					}
					}else{
						Adinfo adinfo = (Adinfo) huandeng.get(i);
						if("2".equals(adinfo.getPicture_site_type())){
						
							
							sb.append("<a href=\""+adinfo.getAdinfo_address()+"\"> <img ")
							.append("src=\""+adinfo.getAdpicture_site()+"\" alt=\""+adinfo.getAdinfo_title()+"\" title=\""+adinfo.getAdinfo_title()+"\"")
							.append("alt=\""+adinfo.getAdinfo_title()+"\" width='312px' height='178px'/></a> ");
							
						}
						if("1".equals(adinfo.getPicture_site_type())){	
						
							sb.append("<a href=\""+adinfo.getAdinfo_address()+"\"> <img ")
							.append("src=\"./pages/topics/aduploadimg/"+adinfo.getAdpicture_site()+"\" alt=\""+adinfo.getAdinfo_title()+"\" title=\""+adinfo.getAdinfo_title()+"\"")
							.append("alt=\""+adinfo.getAdinfo_title()+"\" width='312px' height='178px'/></a> ");
						}
					}
			}
		}
		return sb.toString();
	}



//取联动图片
	private String getLiandong() {
		// TODO Auto-generated method stub
		String hql = " from com.tueye.entity.Adinfo  where adsite.adsite_name='top1' order by adinfo_level desc";
		List liandong = baseHbnDao.queryList(hql);
        StringBuffer sb = new StringBuffer();
		 for(Iterator iter = liandong.iterator() ; iter.hasNext();){
			 Adinfo adinfo = (Adinfo) iter.next();
			 if("2".equals(adinfo.getPicture_site_type())){
				 sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\">" )
				 .append("<img src=\""+adinfo.getAdpicture_site()+"\" alt=\""+adinfo.getAdinfo_title()+"\" title=\""+adinfo.getAdinfo_title()+"\" ")
				 .append("width=\"140\" height=\"140\" /></a></li>");
			 }
			 if("1".equals(adinfo.getPicture_site_type())){
				 sb.append("<li><a href=\""+adinfo.getAdinfo_address()+"\">" )
				 .append("<img src=\"./pages/topics/aduploadimg/"+adinfo.getAdpicture_site()+"\" alt=\""+adinfo.getAdinfo_title()+"\" title=\""+adinfo.getAdinfo_title()+"\" ")
				 .append("width=\"140\" height=\"140\" /></a></li>");
			 }
			
		 }
		return sb.toString();
	}
	private String getsql(String site) {
		// TODO Auto-generated method stub
		String hql = " from com.tueye.entity.Adinfo  where adsite.adsite_name='"+site+ "'order by adinfo_level desc";
		return hql;
	}
}
